//
//  DanZiXunLianMainViewController.swift
//  CKC
//
//  Created by 张振东 on 16/9/28.
//  Copyright © 2016年 ZZD. All rights reserved.
//

import UIKit
// FIXME: comparison operators with optionals were removed from the Swift Standard Libary.
// Consider refactoring the code to use the non-optional operators.
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l < r
  case (nil, _?):
    return true
  default:
    return false
  }
}


class DanZiXunLianMainViewController: UIViewController,UITextFieldDelegate {

    //获取模拟器或者真机的宽度和高度
    let screenWidth = UIScreen.main.bounds.width
    let screenHeight = UIScreen.main.bounds.height
    
    //定义scrollView对象
    var scrollView:UIScrollView?

    //定义一个变量,用于存储模式类别
    var moShiLeiBie:String?
    
    //定义一个变量,用于存储传递过来的分钟值
    var fenZhongZhi:String?
    
    //定义三个ImageView
    var zanTingImageView:UIImageView?
    var chaKanJieGuoImageView:UIImageView?
    var jieShuImageView:UIImageView?
    
    //定义两个View对象
    var firstFenGeXianView:UIView?
    var secondFenGeXianView:UIView?
    
    //定义三个按钮
    var zanTingBtn:UIButton?
    var chaKanJieGuoBtn:UIButton?
    var jieShuBtn:UIButton?
    
    //定义一条横线UIView
    var hengXianView:UIView?
    
    //创建8个UILabel对象
    var zhengQueShuLabel:UILabel?
    var zhengQueShuLabelValue:UILabel?
    
    var zhengQueLvLabel:UILabel?
    var zhengQueLvLabelValue:UILabel?
    
    var shiJianLabel:UILabel?
    var shiJianLabelValue:UILabel?
    
    var suDuLabel:UILabel?
    var suDuLabelValue:UILabel?
    
    //定义三个UIView,用于分割成四个模块
    var leftView:UIView?
    var middleView:UIView?
    var rightView:UIView?
    
    //定义一条横线UIView
    var secondHengXianView:UIView?
    
    //定义一个变量,用于存储正确数
    var zhengQueShu:Int = 0
    
    //自己制作键盘,定义一个UIview对象
    var jianPanView:UIView?
    
    //定义11个按钮分别为0,1,2,3,4,5,6,7,8,9,完成
    var oneBtn:UIButton?
    var twoBtn:UIButton?
    var threeBtn:UIButton?
    var fourBtn:UIButton?
    var fiveBtn:UIButton?
    var sixBtn:UIButton?
    var sevenBtn:UIButton?
    var eightBtn:UIButton?
    var nineBtn:UIButton?
    var dianBtn:UIButton?
    var lingBtn:UIButton?
    
    //创建第一层的三个label对象
    var firstFloorLeftImagView:UIImageView?
    var firstFloorMiddleImageView:UIImageView?
    var firstFloorRightImageView:UIImageView?
    
    //创建20个按钮
    var ziCiBtn:UIButton?
    var fuHaoBtn:UIButton?
    var wenHaoBtn:UIButton?
    var douHaoJuHaoBtn:UIButton?
    var ziMuBtn:UIButton?
    var shanChuBtn:UIButton?
    var numberBtn:UIButton?
    var qiuBtn:UIButton?
    
    //定义两个按钮:保存,下一题
    var saveBtn:UIButton?
    var nextQuestionBtn:UIButton?
    
    //定义三个image
    var firstFloorLeftImage:UIImage?
    var firstFloorMiddleImage:UIImage?
    var firstFloorRightImage:UIImage?
    
    //定义一个imageView,用于存储随机产生的图片
    var danZiImageView:UIImageView?

    //创建一个UITextField对象,用于保存用户输入的数字
    var userInputNumber:UITextField?
    
    //定义一个变量,用于存储从文件夹中读取的图片资源列表
    var imageList:[String]?
    
    //定义一个error对象
    var nsError:NSError?
    
    //定义一个变量,用于保存随机生成的图片的名称
    var imageName:String?
    
    //定义一个变量,用于保存随机生成的图片的名称的长度
    var imageNameCount:Int = 0
    
    //定义一个变量,用于保存输入错误数字的次数
    var shuRuCuoWuCiShu:Int = 0
    
    //定义一个变量,用于存储所有整数
    var suoYouZhengShu:Int = 0
    
    //定义一个变量,用于存储正确率
    var zhengQueLv:Double = 0.0
    
    //定义一个变量,用于存储小键盘训练速度
    var danZiLianXiSuDu = 0
    
    //定义一个定时器
    var timer:Timer!
    
    //定义一个变量:保存计时器时间的值
    var timerValue:Int = 0
    
    //定义一个变量,用于保存弹出的改字对应的纵横码
    var tanChuZhengQueBiXingCode:String = ""
    
    override func viewDidLoad() {
        fengZhuangViewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    func fengZhuangViewDidLoad(){
        
        //设置viewController上面的标题
        if let moShiLeiBie = moShiLeiBie{
            self.title = "\(moShiLeiBie)模式"
            self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white,NSFontAttributeName:UIFont(name: "STHeitiSC-Medium", size: 20.0)!]

            //修改导航栏按钮颜色
            self.navigationController?.navigationBar.tintColor = UIColor.white
            self.navigationController?.navigationBar.barTintColor =
                UIColor(red: 44/255, green: 62/255, blue: 80/255, alpha: 1)
        }
        
        self.view.backgroundColor = UIColor(red: 232/255.0,green:232/255.0,blue:232/255.0,alpha:1)
        let leftBarButton = UIButton(frame:CGRect(x: 0,y: 0,width: 20,height: 20))
        leftBarButton.setBackgroundImage(UIImage(named: "fanhui"), for: UIControlState())
        leftBarButton.addTarget(self, action: #selector(backToPrevious), for: .touchUpInside)
        let leftBarButtonItem = UIBarButtonItem(customView: leftBarButton)
        self.navigationItem.leftBarButtonItems = [leftBarButtonItem]
        
        //初始化scrollView
        scrollView = UIScrollView(frame:CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight))
        scrollView?.backgroundColor = UIColor.white
        scrollView?.contentSize = CGSize(width: screenWidth, height: screenHeight)
        scrollView?.indicatorStyle = UIScrollViewIndicatorStyle.white
        scrollView?.showsVerticalScrollIndicator = true
        scrollView?.showsHorizontalScrollIndicator = false
        
        
        //初始化三个imageView
        zanTingImageView = UIImageView(frame:CGRect(x:(screenWidth/3-30)/2,y:5,width:30,height:30))
        zanTingImageView!.contentMode = .scaleAspectFit
        let zanTingImage = UIImage(named: "zanting")
        zanTingImageView!.image = zanTingImage
        
        chaKanJieGuoImageView = UIImageView(frame:CGRect(x:(screenWidth/3)+(screenWidth/3-30)/2,y:5,width:30,height:30))
        chaKanJieGuoImageView!.contentMode = .scaleAspectFit
        let chaKanChengJiImage = UIImage(named: "chakanchengji")
        chaKanJieGuoImageView!.image = chaKanChengJiImage
        
        jieShuImageView = UIImageView(frame:CGRect(x:(screenWidth/3)*2+(screenWidth/3-30)/2,y:5,width:30,height:30))
        jieShuImageView!.contentMode = .scaleAspectFit
        let jieShuImage = UIImage(named: "jieshu")
        jieShuImageView!.image = jieShuImage
        
        //初始化两个View对象
        firstFenGeXianView = UIView(frame:CGRect(x:screenWidth/3,y:3,width:1,height:60))
        firstFenGeXianView!.backgroundColor = UIColor(red: 200/255,green: 199/255,blue: 204/255,alpha: 1)
        
        secondFenGeXianView = UIView(frame:CGRect(x:(screenWidth/3)*2,y:3,width:1,height:60))
        secondFenGeXianView!.backgroundColor = UIColor(red: 200/255,green: 199/255,blue: 204/255,alpha: 1)
        
        //初始化三个按钮
        zanTingBtn = UIButton(frame:CGRect(x: (screenWidth/3-80)/2,y: 5+30+5,width: 80,height: 20))
        
        zanTingBtn?.setTitle("暂停", for: UIControlState())
        zanTingBtn?.setTitleColor(UIColor.black, for: UIControlState())
        zanTingBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        chaKanJieGuoBtn = UIButton(frame:CGRect(x: (screenWidth/3)+(screenWidth/3-80)/2,y: 5+30+5,width: 80,height: 20))
        
        chaKanJieGuoBtn?.setTitle("查看结果", for: UIControlState())
        chaKanJieGuoBtn?.setTitleColor(UIColor.black, for: UIControlState())
        chaKanJieGuoBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        chaKanJieGuoBtn?.isUserInteractionEnabled = false
        
        jieShuBtn = UIButton(frame:CGRect(x: (screenWidth/3)*2+(screenWidth/3-80)/2,y: 5+30+5,width: 80,height: 20))
        
        jieShuBtn?.setTitle("结束", for: UIControlState())
        
        jieShuBtn?.setTitleColor(UIColor.black, for: UIControlState())
        jieShuBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        //初始化横线UIView
        hengXianView = UIView(frame:CGRect(x: 10,y: 67,width: screenWidth-10,height: 1))
        hengXianView?.backgroundColor = UIColor(red: 200/255,green: 199/255,blue: 204/255,alpha: 1)
        
        //初始化8个UIlabel对象
        //初始化第一个UIlabel对象
        zhengQueShuLabel = UILabel(frame:CGRect(x: (screenWidth/4-60)/2,y: 70,width: 60,height: 20))
        fengZhuangLabelAttributes(zhengQueShuLabel!)
        
        
        //初始化第二个UIlabel对象
        zhengQueShuLabelValue = UILabel(frame:CGRect(x: (screenWidth/4-60)/2,y: 70+20+5,width: 60,height: 20))
        fengZhuangLabelAttributes(zhengQueShuLabelValue!)
        
        //初始化第三个UILabel对象
        zhengQueLvLabel = UILabel(frame:CGRect(x: screenWidth/4+(screenWidth/4-60)/2,y: 70,width: 60,height: 20))
        fengZhuangLabelAttributes(zhengQueLvLabel!)
        
        //初始化第四个UILabel对象
        zhengQueLvLabelValue = UILabel(frame:CGRect(x: screenWidth/4+(screenWidth/4-60)/2,y: 70+20+5,width: 60,height: 20))
        fengZhuangLabelAttributes(zhengQueLvLabelValue!)
        
        //初始化第五个UIlabel对象
        shiJianLabel = UILabel(frame:CGRect(x: screenWidth/2+(screenWidth/4-60)/2,y: 70,width: 60,height: 20))
        fengZhuangLabelAttributes(shiJianLabel!)
        
        //初始化第六个UIlabel对象
        shiJianLabelValue = UILabel(frame:CGRect(x: screenWidth/2+(screenWidth/4-60)/2,y: 70+20+5,width: 60,height: 20))
        fengZhuangLabelAttributes(shiJianLabelValue!)
        
        //初始化第七个UIlabel对象
        suDuLabel = UILabel(frame:CGRect(x: (screenWidth/4)*3+(screenWidth/4-60)/2,y: 70,width: 60,height: 20))
        fengZhuangLabelAttributes(suDuLabel!)
        
        //初始化第八个UIlabel对象
        suDuLabelValue = UILabel(frame:CGRect(x: (screenWidth/4)*3+(screenWidth/4-60)/2,y: 70+20+5,width: 60,height: 20))
        fengZhuangLabelAttributes(suDuLabelValue!)
        
        //初始化leftView
        leftView = UIView(frame:CGRect(x:screenWidth/4,y:70,width:1,height:50))
        leftView?.backgroundColor = UIColor(red: 200/255,green: 199/255,blue: 204/255,alpha: 1)
        
        //初始化middleView
        middleView = UIView(frame:CGRect(x: screenWidth/2,y:70,width:1,height:50))
        middleView?.backgroundColor = UIColor(red: 200/255,green: 199/255,blue: 204/255,alpha: 1)
        
        //初始化rightView
        rightView = UIView(frame:CGRect(x: (screenWidth/4)*3,y:70,width:1,height:50))
        rightView?.backgroundColor = UIColor(red: 200/255,green: 199/255,blue: 204/255,alpha: 1)
        
        //初始化第二条横线View
        secondHengXianView = UIView(frame:CGRect(x: 10,y: 70+50+2,width: screenWidth-10,height: 0.5))
        secondHengXianView?.backgroundColor = UIColor(red: 200/255,green: 199/255,blue: 204/255,alpha: 1)
        
        
        //初始化键盘View
        jianPanView = UIView(frame:CGRect(x: 0, y: screenHeight-255, width: screenWidth, height: 220))
        jianPanView?.backgroundColor = UIColor(red: 209/255,green: 213/255,blue: 219/255,alpha: 1)
        jianPanView?.isUserInteractionEnabled = true
        
        //初始化第一层的三个label对象
        firstFloorLeftImagView = UIImageView(frame:CGRect(x: 0,y: 0,width: 60,height: 40))
        firstFloorLeftImage = UIImage(named: "firstFloorLeftImage.gif")
        firstFloorLeftImagView?.image = firstFloorLeftImage
        
        
        firstFloorMiddleImageView = UIImageView(frame:CGRect(x: 60,y: 0,width: screenWidth-80,height: 40))
        firstFloorMiddleImage = UIImage(named: "firstFloorMiddleImage.gif")
        firstFloorMiddleImageView?.image = firstFloorMiddleImage
        
        
        firstFloorRightImageView = UIImageView(frame:CGRect(x: screenWidth-40,y: 0,width: 40,height: 40))
        firstFloorRightImage = UIImage(named: "firstFloorRightImage.gif")
        firstFloorRightImageView?.image = firstFloorRightImage
        
        
        //初始化20个按钮
        ziCiBtn = UIButton(frame:CGRect(x: 0,y: 40+5,width: 50,height: 40))
        ziCiBtn?.setTitle("字/词", for: UIControlState())
        ziCiBtn?.setImage(UIImage(named: "ziCiImage.gif"), for: UIControlState())
        
        sevenBtn = UIButton(frame:CGRect(x: 50+5,y: 40+5,width: (screenWidth-120)/3,height: 40))
        sevenBtn?.layer.cornerRadius = 10
        sevenBtn?.backgroundColor = UIColor.white
        sevenBtn?.setTitle("7", for: UIControlState())
        sevenBtn?.isUserInteractionEnabled = true
        sevenBtn?.setTitleColor(UIColor(red: 0,green: 0,blue: 0,alpha: 1), for: UIControlState())
        
        eightBtn = UIButton(frame:CGRect(x: 50+5*2+(screenWidth-120)/3,y: 40+5,width: (screenWidth-120)/3,height: 40))
        eightBtn?.setTitleColor(UIColor(red: 0,green: 0,blue: 0,alpha: 1), for: UIControlState())
        eightBtn?.backgroundColor = UIColor.white
        eightBtn?.layer.cornerRadius = 10
        eightBtn?.setTitle("8", for: UIControlState())
        eightBtn?.isUserInteractionEnabled = true
        
        nineBtn = UIButton(frame:CGRect(x: 50+5*3+2*(screenWidth-120)/3,y: 40+5,width: (screenWidth-120)/3,height: 40))
        nineBtn?.titleLabel?.textColor = UIColor(red: 0,green: 0,blue: 0,alpha: 1)
        nineBtn?.backgroundColor = UIColor.white
        nineBtn?.setTitleColor(UIColor(red: 0,green: 0,blue: 0,alpha: 1), for: UIControlState())
        
        nineBtn?.layer.cornerRadius = 10
        nineBtn?.setTitle("9", for: UIControlState())
        nineBtn?.isUserInteractionEnabled = true
        
        fuHaoBtn = UIButton(frame:CGRect(x: screenWidth-50,y: 40+5,width: 50,height: 40))
        fuHaoBtn?.setTitle("符", for: UIControlState())
        fuHaoBtn?.setImage(UIImage(named: "fuHaoImage"), for: UIControlState())
        
        wenHaoBtn = UIButton(frame:CGRect(x: 0,y: 40+5+40+5,width: 50,height: 40))
        wenHaoBtn?.setTitle("?", for: UIControlState())
        wenHaoBtn?.setImage(UIImage(named: "wenHaoImage.gif"), for: UIControlState())
        
        
        fourBtn = UIButton(frame:CGRect(x: 50+5,y: 40+5+40+5,width: (screenWidth-120)/3,height: 40))
        fourBtn?.setTitleColor(UIColor(red: 0,green: 0,blue: 0,alpha: 1), for: UIControlState())
        fourBtn?.backgroundColor = UIColor.white
        fourBtn?.layer.cornerRadius = 10
        fourBtn?.setTitle("4", for: UIControlState())
        fourBtn?.isUserInteractionEnabled = true
        
        
        fiveBtn = UIButton(frame:CGRect(x: 50+5*2+(screenWidth-120)/3,y: 40+5+40+5,width: (screenWidth-120)/3,height: 40))
        fiveBtn?.setTitleColor(UIColor(red: 0,green: 0,blue: 0,alpha: 1), for: UIControlState())
        fiveBtn?.backgroundColor = UIColor.white
        fiveBtn?.layer.cornerRadius = 10
        fiveBtn?.setTitle("5", for: UIControlState())
        fiveBtn?.isUserInteractionEnabled = true
        
        sixBtn = UIButton(frame:CGRect(x: 50+5*3+2*(screenWidth-120)/3,y: 40+5+40+5,width: (screenWidth-120)/3,height: 40))
        sixBtn?.setTitleColor(UIColor(red: 0,green: 0,blue: 0,alpha: 1), for: UIControlState())
        sixBtn?.backgroundColor = UIColor.white
        sixBtn?.layer.cornerRadius = 10
        sixBtn?.setTitle("6", for: UIControlState())
        sixBtn?.isUserInteractionEnabled = true
        
        douHaoJuHaoBtn = UIButton(frame:CGRect(x: screenWidth-50,y: 40+5+40+5,width: 50,height: 40))
        douHaoJuHaoBtn?.setTitle("，。", for: UIControlState())
        douHaoJuHaoBtn?.setImage(UIImage(named: "douHaoJuHaoImage.gif"), for: UIControlState())
        
        ziMuBtn = UIButton(frame:CGRect(x: 0,y: 40+5+40+5+40+5,width: 50,height: 40))
        ziMuBtn?.setTitle("ABC", for: UIControlState())
        ziMuBtn?.setImage(UIImage(named: "ziMuImage.gif"), for: UIControlState())
        
        oneBtn = UIButton(frame:CGRect(x: 50+5,y: 40+5+40+5+40+5,width: (screenWidth-120)/3,height: 40))
        oneBtn?.setTitleColor(UIColor(red: 0,green: 0,blue: 0,alpha: 1), for: UIControlState())
        oneBtn?.backgroundColor = UIColor.white
        oneBtn?.layer.cornerRadius = 10
        oneBtn?.setTitle("1", for: UIControlState())
        oneBtn?.isUserInteractionEnabled = true
        
        twoBtn = UIButton(frame:CGRect(x: 50+5*2+(screenWidth-120)/3,y: 40+5+40+5+40+5,width: (screenWidth-120)/3,height: 40))
        twoBtn?.setTitleColor(UIColor(red: 0,green: 0,blue: 0,alpha: 1), for: UIControlState())
        twoBtn?.backgroundColor = UIColor.white
        twoBtn?.layer.cornerRadius = 10
        twoBtn?.setTitle("2", for: UIControlState())
        twoBtn?.isUserInteractionEnabled = true
        
        threeBtn = UIButton(frame:CGRect(x: 50+5*3+2*(screenWidth-120)/3,y: 40+5+40+5+40+5,width: (screenWidth-120)/3,height: 40))
        threeBtn?.setTitleColor(UIColor(red: 0,green: 0,blue: 0,alpha: 1), for: UIControlState())
        threeBtn?.backgroundColor = UIColor.white
        threeBtn?.layer.cornerRadius = 10
        threeBtn?.setTitle("3", for: UIControlState())
        threeBtn?.isUserInteractionEnabled = true
        
        shanChuBtn = UIButton(frame:CGRect(x: screenWidth-50,y: 40+5+40+5+40+5,width: 50,height: 40))
        shanChuBtn?.setTitle("<", for: UIControlState())
        shanChuBtn?.setImage(UIImage(named: "qingChuImage.gif"), for: UIControlState())
        
        numberBtn = UIButton(frame:CGRect(x: 0,y: 40+5+40+5+40+5+40+5,width: 50,height: 40))
        numberBtn?.setTitle("123", for: UIControlState())
        numberBtn?.setImage(UIImage(named: "numberImage.gif"), for: UIControlState())
        
        qiuBtn = UIButton(frame:CGRect(x: 50+5,y: 40+5+40+5+40+5+40+5,width: 50,height: 40))
        qiuBtn?.setImage(UIImage(named: "qiuImage.gif"), for: UIControlState())
        
        lingBtn = UIButton(frame:CGRect(x: 50+5+50+5,y: 40+5+40+5+40+5+40+5,width: (screenWidth-120)/3,height: 40))
        lingBtn?.setTitleColor(UIColor(red: 0,green: 0,blue: 0,alpha: 1), for: UIControlState())
        lingBtn?.backgroundColor = UIColor.white
        lingBtn?.layer.cornerRadius = 10
        lingBtn?.setTitle("0", for: UIControlState())
        lingBtn?.isUserInteractionEnabled = true
        
        saveBtn = UIButton(frame:CGRect(x: 50+5+50+5+5+(screenWidth-120)/3,y: 40+5+40+5+40+5+40+5,width: (screenWidth-120)/3,height: 40))
        saveBtn?.setTitleColor(UIColor(red: 0,green: 0,blue: 0,alpha: 1), for: UIControlState())
        saveBtn?.backgroundColor = UIColor.white
        saveBtn?.layer.cornerRadius = 10
        saveBtn?.setTitle("空格", for: UIControlState())
        
        nextQuestionBtn = UIButton(frame:CGRect(x: 50+5+50+5+5+5+2*(screenWidth-120)/3,y: 40+5+40+5+40+5+40+5,width: (screenWidth-120)/3,height: 40))
        nextQuestionBtn?.backgroundColor = UIColor(red: 170/255,green: 170/255,blue: 170/255,alpha: 1)
        nextQuestionBtn?.setTitleColor(UIColor(red: 0,green: 0,blue: 0,alpha: 1), for: UIControlState())
        nextQuestionBtn?.layer.cornerRadius = 10
        nextQuestionBtn?.setTitle("换行", for: UIControlState())
        
        jianPanView?.addSubview(firstFloorLeftImagView!)
        jianPanView?.addSubview(firstFloorMiddleImageView!)
        jianPanView?.addSubview(firstFloorRightImageView!)
        jianPanView?.addSubview(ziCiBtn!)
        jianPanView?.addSubview(sevenBtn!)
        jianPanView?.addSubview(eightBtn!)
        jianPanView?.addSubview(nineBtn!)
        jianPanView?.addSubview(fuHaoBtn!)
        jianPanView?.addSubview(wenHaoBtn!)
        jianPanView?.addSubview(fourBtn!)
        jianPanView?.addSubview(fiveBtn!)
        jianPanView?.addSubview(sixBtn!)
        jianPanView?.addSubview(douHaoJuHaoBtn!)
        jianPanView?.addSubview(ziMuBtn!)
        jianPanView?.addSubview(oneBtn!)
        jianPanView?.addSubview(twoBtn!)
        jianPanView?.addSubview(threeBtn!)
        jianPanView?.addSubview(shanChuBtn!)
        jianPanView?.addSubview(numberBtn!)
        jianPanView?.addSubview(qiuBtn!)
        jianPanView?.addSubview(lingBtn!)
        jianPanView?.addSubview(saveBtn!)
        jianPanView?.addSubview(nextQuestionBtn!)

        //初始化笔形ImageView
        danZiImageView = UIImageView(frame:CGRect(x: 70,y:70+50+5,width:screenWidth-140,height:screenWidth-180))
        danZiImageView!.contentMode = .scaleAspectFit
        danZiImageView!.backgroundColor = UIColor(red: 192/255,green: 192/255,blue: 192/255,alpha: 1)
        danZiImageView?.isUserInteractionEnabled = true
        
        
        //长按手势
        let longPressGesutre = UILongPressGestureRecognizer(target: self, action: #selector(handleLongpressGesture))
        //长按时间为1秒
        longPressGesutre.minimumPressDuration = 1
        //允许15秒运动
        longPressGesutre.allowableMovement = 15
        //所需触摸1次
        longPressGesutre.numberOfTouchesRequired = 1
        danZiImageView!.addGestureRecognizer(longPressGesutre)
        
        let str = Bundle.main.path(forResource: "caiimage",ofType: nil, inDirectory: nil)
        
        print(str as Any)
        //从制定的目录下面读取文件
        do{
            try imageList = FileManager.default.contentsOfDirectory(atPath: str!)
            if imageList != nil{
                print(imageList as Any)
            }
        }
        catch let nsError as NSError{
            print(nsError)
        }
  
        //初始化userInputNumber对象
        userInputNumber = UITextField(frame:CGRect(x: 70,y:70+50+10+screenWidth-180,width:screenWidth-140,height:40))
        userInputNumber!.backgroundColor = UIColor(red: 192/255,green: 192/255,blue: 192/255,alpha: 1)
        userInputNumber?.textAlignment = .center
        userInputNumber?.font = UIFont.systemFont(ofSize: 20)
        userInputNumber!.delegate = self
        
        //给0、1、2、3、4、5、6、7、8、9、下一个添加点击事件
        oneBtn?.addTarget(self, action: #selector(clickOne), for: .touchUpInside)
        twoBtn?.addTarget(self, action: #selector(clickTwo), for: .touchUpInside)
        threeBtn?.addTarget(self, action: #selector(clickThree), for: .touchUpInside)
        fourBtn?.addTarget(self, action: #selector(clickFour), for: .touchUpInside)
        fiveBtn?.addTarget(self, action: #selector(clickFive), for: .touchUpInside)
        sixBtn?.addTarget(self, action: #selector(clickSix), for: .touchUpInside)
        sevenBtn?.addTarget(self, action: #selector(clickSeven), for: .touchUpInside)
        eightBtn?.addTarget(self, action: #selector(clickEight), for: .touchUpInside)
        nineBtn?.addTarget(self, action: #selector(clickNine), for: .touchUpInside)
        lingBtn?.addTarget(self, action: #selector(clickZero), for: .touchUpInside)
       
        if let moShiCategory = moShiLeiBie{
            print(moShiCategory)
            if moShiCategory == "计时"{
                timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(genJuSelectedValueJiShi), userInfo: nil, repeats: true)
            }else if moShiCategory == "定时"{
                //将传递过来的分钟值转换为秒数
                timerValue = 60 * Int(fenZhongZhi!)!
                
                timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(genJuXuanZhongFenZhongZhiDaoJiShi), userInfo: nil, repeats: true)
            }
        }

        //分别给三个按钮添加点击事件
        zanTingBtn!.addTarget(self,action:#selector(zanTing),for:.touchUpInside)
        chaKanJieGuoBtn!.addTarget(self,action:#selector(chaKanJieGuo),for:.touchUpInside)
        jieShuBtn!.addTarget(self,action:#selector(jieShu),for:.touchUpInside)
        
        scrollView?.addSubview(firstFenGeXianView!)
        scrollView?.addSubview(secondFenGeXianView!)
        scrollView?.addSubview(zanTingBtn!)
        scrollView?.addSubview(chaKanJieGuoBtn!)
        scrollView?.addSubview(jieShuBtn!)
        scrollView?.addSubview(hengXianView!)
        scrollView?.addSubview(zanTingImageView!)
        scrollView?.addSubview(chaKanJieGuoImageView!)
        scrollView?.addSubview(jieShuImageView!)
        scrollView?.addSubview(zhengQueShuLabel!)
        scrollView?.addSubview(zhengQueLvLabel!)
        scrollView?.addSubview(shiJianLabel!)
        scrollView?.addSubview(suDuLabel!)
        scrollView?.addSubview(zhengQueShuLabelValue!)
        scrollView?.addSubview(zhengQueLvLabelValue!)
        scrollView?.addSubview(shiJianLabelValue!)
        scrollView?.addSubview(suDuLabelValue!)
        scrollView?.addSubview(leftView!)
        scrollView?.addSubview(middleView!)
        scrollView?.addSubview(rightView!)
        scrollView?.addSubview(secondHengXianView!)
        scrollView?.addSubview(danZiImageView!)
        scrollView!.addSubview(jianPanView!)
        scrollView?.addSubview(userInputNumber!)
        
        self.view.addSubview(scrollView!)
        shengChengTuPianBingFuZhi()
        super.viewDidLoad()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        if let moShiLeiBie = moShiLeiBie{
            self.title = "\(moShiLeiBie)模式"
        }
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white,NSFontAttributeName:UIFont(name: "STHeitiSC-Medium", size: 20.0)!]
    }
    

    
    /*
     作者:张振东
     时间:2016-10-06
     功能:长按屏幕显示正确的纵横码
     */
    func handleLongpressGesture(_ sender : UILongPressGestureRecognizer){
        print("张振东")
        if sender.state == UIGestureRecognizerState.began{
            let dxAlertView:DXAlertView = DXAlertView(title: "温馨提示", contentText: tanChuZhengQueBiXingCode, leftButtonTitle: nil, rightButtonTitle: "好的")
            dxAlertView.show()
        }
        
    }

    /*
     作者:张振东
     时间:2016-9-25
     功能:实现图片的随记生成
     1、将可选的[String]数组强制类型转换
     2、获得该[String]数组的长度
     3、随记生成一个0-[String]数组的长度的整数
     4、通过第三步得到的数字可以获得该数组对应的值(也就是图片的名字)
     5、给图片imageView设置图片
     */
    func shengChengTuPianBingFuZhi(){
        if let cancelImageListOptional = imageList{
            print(cancelImageListOptional)
            //获得该[String]数组的长度
            let arrayOfLength = cancelImageListOptional.count
            print(arrayOfLength)
            //随记生成一个0-[String]数组的长度的整数
            let randomNumber:UInt32 = arc4random_uniform(UInt32(arrayOfLength))
            print(randomNumber)
            //通过第三步得到的数字可以获得该数组对应的值(也就是图片的名字)
            imageName = cancelImageListOptional[Int(randomNumber)]
            
            tanChuZhengQueBiXingCode = jieQuImageNameDeDaoDianZhiQian(imageName!)
            print(tanChuZhengQueBiXingCode)
            //给图片imageView设置图片
            danZiImageView?.image = UIImage(named: imageName!)
            jieQuImageNameBingFanHuiChangDu()
        }
    }
    
    /*
     作者:张振东
     时间:2016-9-28
     功能:截取字符串并返回该字符串的长度
    */
    @discardableResult
    func jieQuImageNameBingFanHuiChangDu() -> Int{
        //使用","切割字符串
        let imageLists = imageName?.components(separatedBy: ".")
        print(imageLists as Any)
        print(imageLists![0])
        if imageLists![0].components(separatedBy: "X").count > 1{
            //说明包含
            print("包含X")
            let range: Range<String.Index> = imageLists![0].range(of: "X")!
            
            let endIndex: Int = imageLists![0].characters.distance(from: imageLists![0].startIndex, to: range.lowerBound)
            let subString = (imageLists![0] as NSString).substring(with: NSMakeRange(0,endIndex))
            print(subString.characters.count)
            imageNameCount = subString.characters.count
            }else{
            
            //说明不包含
            print(imageLists![0].characters.count)
            imageNameCount = imageLists![0].characters.count
        }
        return imageNameCount
    }
    
    /*
     作者:张振东
     时间:2016-9-28
     功能:截取字符串并返回字符串点之前的名称的字符串
     */
    func jieQuImageNameDeDaoDianZhiQian(_ imageName:String) -> String{
        //使用","切割字符串
        let imageLists = imageName.components(separatedBy: ".")
        print(imageLists)
        print(imageLists[0])
        
        if imageLists[0].components(separatedBy: "X").count > 1{
            //说明包含
            print("包含X")
            let range: Range<String.Index> = imageLists[0].range(of: "X")!
            
            let endIndex: Int = imageLists[0].characters.distance(from: imageLists[0].startIndex, to: range.lowerBound)
            let subString = (imageLists[0] as NSString).substring(with: NSMakeRange(0,endIndex))
            return subString
        }else{
            //说明不包含
            return "\(imageLists[0])"
        }

    }
    
    /*
     作者:张振东
     时间:2016-9-29
     功能:当点击键盘上的1的时候进行的相关操作
     */
    func clickOne(){
        
        if userInputNumber?.text?.characters.count < imageNameCount{
            userInputNumber?.text = (userInputNumber?.text)! + "1"
        }
        print(imageNameCount)
        if userInputNumber?.text?.characters.count == imageNameCount{
            //如果输入的字符串的长度和生成的图片的名称的长度相等,那么不管是相等还是不等,那么所有数都+1
            suoYouZhengShu = suoYouZhengShu+1
            
            print(userInputNumber?.text?.characters as Any)
            if userInputNumber?.text == jieQuImageNameDeDaoDianZhiQian(imageName!){
                print("zhengque")
                //正确的个数➕1
                zhengQueShu = zhengQueShu+1
                print(zhengQueShu)
                zhengQueShuLabelValue?.text = "\(zhengQueShu)"
                shengChengTuPianBingFuZhi()
                userInputNumber?.backgroundColor = UIColor(red: 192/255,green: 192/255,blue: 192/255,alpha: 1)
                userInputNumber?.text = ""
            }else{
                shuRuCuoWuCiShu = shuRuCuoWuCiShu+1
                userInputNumber?.backgroundColor = UIColor.red
                userInputNumber?.text = ""
                if shuRuCuoWuCiShu == 2{
                    //重新生成新的图片
                    shengChengTuPianBingFuZhi()
                    //背景色恢复正常
                    userInputNumber?.backgroundColor = UIColor(red: 192/255,green: 192/255,blue: 192/255,alpha: 1)
                    shuRuCuoWuCiShu = 0
                    userInputNumber?.text = ""
                }

                print("buzhengque")
            }
            let ZhengQueLv = jiSuanZhengQueLv()
            zhengQueLvLabelValue?.text = "\(ZhengQueLv)%"
        }
        //计算单字练习速度并实时显示
        danZiLianXiSuDu = jiSuanLianXiSuDu()
        suDuLabelValue?.text = "\(danZiLianXiSuDu)字/分钟"
    }
    
    /*
     作者:张振东
     时间:2016-9-28
     功能:当点击键盘上的2的时候进行的相关操作
     */
    func clickTwo(){
        
        if userInputNumber?.text?.characters.count < imageNameCount{
            userInputNumber?.text = (userInputNumber?.text)! + "2"
        }
        print(imageNameCount)
        if userInputNumber?.text?.characters.count == imageNameCount{
            //如果输入的字符串的长度和生成的图片的名称的长度相等,那么不管是相等还是不等,那么所有数都+1
            suoYouZhengShu = suoYouZhengShu+1
            print(userInputNumber?.text?.characters as Any)
            if userInputNumber?.text == jieQuImageNameDeDaoDianZhiQian(imageName!){
                print("zhengque")
                //正确的个数➕1
                zhengQueShu = zhengQueShu+1
                print(zhengQueShu)
                
                zhengQueShuLabelValue?.text = "\(zhengQueShu)"
                shengChengTuPianBingFuZhi()
                userInputNumber?.backgroundColor = UIColor(red: 192/255,green: 192/255,blue: 192/255,alpha: 1)
                userInputNumber?.text = ""
            }else{
                shuRuCuoWuCiShu = shuRuCuoWuCiShu+1
                userInputNumber?.backgroundColor = UIColor.red
                userInputNumber?.text = ""
                if shuRuCuoWuCiShu == 2{
                    //重新生成新的图片
                    shengChengTuPianBingFuZhi()
                    //背景色恢复正常
                    userInputNumber?.backgroundColor = UIColor(red: 192/255,green: 192/255,blue: 192/255,alpha: 1)
                    shuRuCuoWuCiShu = 0
                    userInputNumber?.text = ""
                }

                print("buzhengque")
            }
            let ZhengQueLv = jiSuanZhengQueLv()
            zhengQueLvLabelValue?.text = "\(ZhengQueLv)%"
        }
        
        //计算单字练习速度并实时显示
        danZiLianXiSuDu = jiSuanLianXiSuDu()
        suDuLabelValue?.text = "\(danZiLianXiSuDu)字/分钟"
    }
    
    /*
     作者:张振东
     时间:2016-9-28
     功能:当点击键盘上的3的时候进行的相关操作
     */
    func clickThree(){
        if userInputNumber?.text?.characters.count < imageNameCount{
            userInputNumber?.text = (userInputNumber?.text)! + "3"
        }
        print(imageNameCount)
        if userInputNumber?.text?.characters.count == imageNameCount{
            //如果输入的字符串的长度和生成的图片的名称的长度相等,那么不管是相等还是不等,那么所有数都+1
            suoYouZhengShu = suoYouZhengShu+1
            print(userInputNumber?.text?.characters as Any)
            if userInputNumber?.text == jieQuImageNameDeDaoDianZhiQian(imageName!){
                print("zhengque")
                //正确的个数➕1
                zhengQueShu = zhengQueShu+1
                print(zhengQueShu)
                zhengQueShuLabelValue?.text = "\(zhengQueShu)"
                shengChengTuPianBingFuZhi()
                userInputNumber?.backgroundColor = UIColor(red: 192/255,green: 192/255,blue: 192/255,alpha: 1)
                userInputNumber?.text = ""
            }else{
                shuRuCuoWuCiShu = shuRuCuoWuCiShu+1
                userInputNumber?.backgroundColor = UIColor.red
                userInputNumber?.text = ""
                if shuRuCuoWuCiShu == 2{
                    //重新生成新的图片
                    shengChengTuPianBingFuZhi()
                    //背景色恢复正常
                    userInputNumber?.backgroundColor = UIColor(red: 192/255,green: 192/255,blue: 192/255,alpha: 1)
                    shuRuCuoWuCiShu = 0
                    userInputNumber?.text = ""
                }

                print("buzhengque")
            }
            let ZhengQueLv = jiSuanZhengQueLv()
            zhengQueLvLabelValue?.text = "\(ZhengQueLv)%"
        }
       
        //计算单字练习速度并实时显示
        danZiLianXiSuDu = jiSuanLianXiSuDu()
        suDuLabelValue?.text = "\(danZiLianXiSuDu)字/分钟"
    }
    
    /*
     作者:张振东
     时间:2016-9-29
     功能:当点击键盘上的4的时候进行的相关操作
     */
    func clickFour(){
        if userInputNumber?.text?.characters.count < imageNameCount{
            userInputNumber?.text = (userInputNumber?.text)! + "4"
        }
        print(imageNameCount)
        if userInputNumber?.text?.characters.count == imageNameCount{
            //如果输入的字符串的长度和生成的图片的名称的长度相等,那么不管是相等还是不等,那么所有数都+1
            suoYouZhengShu = suoYouZhengShu+1
            print(userInputNumber?.text?.characters as Any)
            if userInputNumber?.text == jieQuImageNameDeDaoDianZhiQian(imageName!){
                print("zhengque")
                //正确的个数➕1
                zhengQueShu = zhengQueShu+1
                print(zhengQueShu)
                zhengQueShuLabelValue?.text = "\(zhengQueShu)"
                shengChengTuPianBingFuZhi()
                userInputNumber?.backgroundColor = UIColor(red: 192/255,green: 192/255,blue: 192/255,alpha: 1)
                userInputNumber?.text = ""
            }else{
                shuRuCuoWuCiShu = shuRuCuoWuCiShu+1
                userInputNumber?.backgroundColor = UIColor.red
                userInputNumber?.text = ""
                if shuRuCuoWuCiShu == 2{
                    //重新生成新的图片
                    shengChengTuPianBingFuZhi()
                    //背景色恢复正常
                    userInputNumber?.backgroundColor = UIColor(red: 192/255,green: 192/255,blue: 192/255,alpha: 1)
                    shuRuCuoWuCiShu = 0
                    userInputNumber?.text = ""
                }

                print("buzhengque")
            }
            let ZhengQueLv = jiSuanZhengQueLv()
            zhengQueLvLabelValue?.text = "\(ZhengQueLv)%"

        }
        //计算单字练习速度并实时显示
        danZiLianXiSuDu = jiSuanLianXiSuDu()
        suDuLabelValue?.text = "\(danZiLianXiSuDu)字/分钟"
    }
    
    /*
     作者:张振东
     时间:2016-9-29
     功能:当点击键盘上的5的时候进行的相关操作
     */
    func clickFive(){
        
        if userInputNumber?.text?.characters.count < imageNameCount{
            userInputNumber?.text = (userInputNumber?.text)! + "5"
        }
        print(imageNameCount)
        if userInputNumber?.text?.characters.count == imageNameCount{
            //如果输入的字符串的长度和生成的图片的名称的长度相等,那么不管是相等还是不等,那么所有数都+1
            suoYouZhengShu = suoYouZhengShu+1
            print(userInputNumber?.text?.characters as Any)
            if userInputNumber?.text == jieQuImageNameDeDaoDianZhiQian(imageName!){
                print("zhengque")
                //正确的个数➕1
                zhengQueShu = zhengQueShu+1
                print(zhengQueShu)
                zhengQueShuLabelValue?.text = "\(zhengQueShu)"
                shengChengTuPianBingFuZhi()
                userInputNumber?.backgroundColor = UIColor(red: 192/255,green: 192/255,blue: 192/255,alpha: 1)
                userInputNumber?.text = ""
            }else{
                shuRuCuoWuCiShu = shuRuCuoWuCiShu+1
                userInputNumber?.backgroundColor = UIColor.red
                userInputNumber?.text = ""
                if shuRuCuoWuCiShu == 2{
                    //重新生成新的图片
                    shengChengTuPianBingFuZhi()
                    //背景色恢复正常
                    userInputNumber?.backgroundColor = UIColor(red: 192/255,green: 192/255,blue: 192/255,alpha: 1)
                    shuRuCuoWuCiShu = 0
                    userInputNumber?.text = ""
                }

                print("buzhengque")
            }
            let ZhengQueLv = jiSuanZhengQueLv()
            zhengQueLvLabelValue?.text = "\(ZhengQueLv)%"
        }
        
        //计算单字练习速度并实时显示
        danZiLianXiSuDu = jiSuanLianXiSuDu()
        suDuLabelValue?.text = "\(danZiLianXiSuDu)字/分钟"

    }
    
    /*
     作者:张振东
     时间:2016-9-29
     功能:当点击键盘上的6的时候进行的相关操作
     */
    func clickSix(){
        if userInputNumber?.text?.characters.count < imageNameCount{
            userInputNumber?.text = (userInputNumber?.text)! + "6"
        }
        print(imageNameCount)
        if userInputNumber?.text?.characters.count == imageNameCount{
            //如果输入的字符串的长度和生成的图片的名称的长度相等,那么不管是相等还是不等,那么所有数都+1
            suoYouZhengShu = suoYouZhengShu+1
            print(userInputNumber?.text?.characters as Any)
            if userInputNumber?.text == jieQuImageNameDeDaoDianZhiQian(imageName!){
                print("zhengque")
                //正确的个数➕1
                zhengQueShu = zhengQueShu+1
                print(zhengQueShu)
                zhengQueShuLabelValue?.text = "\(zhengQueShu)"
                shengChengTuPianBingFuZhi()
                userInputNumber?.backgroundColor = UIColor(red: 192/255,green: 192/255,blue: 192/255,alpha: 1)
                userInputNumber?.text = ""
            }else{
                shuRuCuoWuCiShu = shuRuCuoWuCiShu+1
                userInputNumber?.backgroundColor = UIColor.red
                userInputNumber?.text = ""
                if shuRuCuoWuCiShu == 2{
                    //重新生成新的图片
                    shengChengTuPianBingFuZhi()
                    //背景色恢复正常
                    userInputNumber?.backgroundColor = UIColor(red: 192/255,green: 192/255,blue: 192/255,alpha: 1)
                    shuRuCuoWuCiShu = 0
                    userInputNumber?.text = ""
                    
                }

                print("buzhengque")
            }
            let ZhengQueLv = jiSuanZhengQueLv()
            zhengQueLvLabelValue?.text = "\(ZhengQueLv)%"
        }
        
        //计算单字练习速度并实时显示
        danZiLianXiSuDu = jiSuanLianXiSuDu()
        suDuLabelValue?.text = "\(danZiLianXiSuDu)字/分钟"
    }
    
    /*
     作者:张振东
     时间:2016-9-29
     功能:当点击键盘上的7的时候进行的相关操作
     */
    func clickSeven(){
        if userInputNumber?.text?.characters.count < imageNameCount{
            userInputNumber?.text = (userInputNumber?.text)! + "7"
        }
        print(imageNameCount)
        if userInputNumber?.text?.characters.count == imageNameCount{
            //如果输入的字符串的长度和生成的图片的名称的长度相等,那么不管是相等还是不等,那么所有数都+1
            suoYouZhengShu = suoYouZhengShu+1
            print(userInputNumber?.text?.characters as Any)
            if userInputNumber?.text == jieQuImageNameDeDaoDianZhiQian(imageName!){
                print("zhengque")
                //正确的个数➕1
                zhengQueShu = zhengQueShu+1
                print(zhengQueShu)
                zhengQueShuLabelValue?.text = "\(zhengQueShu)"
                shengChengTuPianBingFuZhi()
                userInputNumber?.backgroundColor = UIColor(red: 192/255,green: 192/255,blue: 192/255,alpha: 1)
                userInputNumber?.text = ""
            }else{
                shuRuCuoWuCiShu = shuRuCuoWuCiShu+1
                userInputNumber?.backgroundColor = UIColor.red
                userInputNumber?.text = ""
                if shuRuCuoWuCiShu == 2{
                    //重新生成新的图片
                    shengChengTuPianBingFuZhi()
                    //背景色恢复正常
                    userInputNumber?.backgroundColor = UIColor(red: 192/255,green: 192/255,blue: 192/255,alpha: 1)
                    shuRuCuoWuCiShu = 0
                    userInputNumber?.text = ""
                }

                print("buzhengque")
            }
            let ZhengQueLv = jiSuanZhengQueLv()
            zhengQueLvLabelValue?.text = "\(ZhengQueLv)%"
        }
        
        //计算单字练习速度并实时显示
        danZiLianXiSuDu = jiSuanLianXiSuDu()
        suDuLabelValue?.text = "\(danZiLianXiSuDu)字/分钟"
    }
    
    /*
     作者:张振东
     时间:2016-9-29
     功能:当点击键盘上的8的时候进行的相关操作
     */
    func clickEight(){
        
        if userInputNumber?.text?.characters.count < imageNameCount{
            userInputNumber?.text = (userInputNumber?.text)! + "8"
        }
        print(imageNameCount)
        if userInputNumber?.text?.characters.count == imageNameCount{
            //如果输入的字符串的长度和生成的图片的名称的长度相等,那么不管是相等还是不等,那么所有数都+1
            suoYouZhengShu = suoYouZhengShu+1
            
            print(userInputNumber?.text?.characters as Any)
            if userInputNumber?.text == jieQuImageNameDeDaoDianZhiQian(imageName!){
                print("zhengque")
                //正确的个数➕1
                zhengQueShu = zhengQueShu+1
                print(zhengQueShu)
                zhengQueShuLabelValue?.text = "\(zhengQueShu)"
                shengChengTuPianBingFuZhi()
                userInputNumber?.backgroundColor = UIColor(red: 192/255,green: 192/255,blue: 192/255,alpha: 1)
                userInputNumber?.text = ""
            }else{
                shuRuCuoWuCiShu = shuRuCuoWuCiShu+1
                userInputNumber?.backgroundColor = UIColor.red
                userInputNumber?.text = ""
                if shuRuCuoWuCiShu == 2{
                    //重新生成新的图片
                    shengChengTuPianBingFuZhi()
                    //背景色恢复正常
                    userInputNumber?.backgroundColor = UIColor(red: 192/255,green: 192/255,blue: 192/255,alpha: 1)
                    shuRuCuoWuCiShu = 0
                    userInputNumber?.text = ""
                }

                print("buzhengque")
            }
            let ZhengQueLv = jiSuanZhengQueLv()
            zhengQueLvLabelValue?.text = "\(ZhengQueLv)%"

        }
        //计算单字练习速度并实时显示
        danZiLianXiSuDu = jiSuanLianXiSuDu()
        suDuLabelValue?.text = "\(danZiLianXiSuDu)字/分钟"
    }
    
    /*
     作者:张振东
     时间:2016-9-29
     功能:当点击键盘上的9的时候进行的相关操作
     */
    func clickNine(){
        
        if userInputNumber?.text?.characters.count < imageNameCount{
            userInputNumber?.text = (userInputNumber?.text)! + "9"
        }
        print(imageNameCount)
        if userInputNumber?.text?.characters.count == imageNameCount{
            //如果输入的字符串的长度和生成的图片的名称的长度相等,那么不管是相等还是不等,那么所有数都+1
            suoYouZhengShu = suoYouZhengShu+1
            print(userInputNumber?.text?.characters as Any)
            if userInputNumber?.text == jieQuImageNameDeDaoDianZhiQian(imageName!){
                print("zhengque")
                //正确的个数➕1
                zhengQueShu = zhengQueShu+1
                print(zhengQueShu)
                zhengQueShuLabelValue?.text = "\(zhengQueShu)"
                shengChengTuPianBingFuZhi()
                userInputNumber?.backgroundColor = UIColor(red: 192/255,green: 192/255,blue: 192/255,alpha: 1)
                userInputNumber?.text = ""
            }else{
                shuRuCuoWuCiShu = shuRuCuoWuCiShu+1
                userInputNumber?.backgroundColor = UIColor.red
                userInputNumber?.text = ""
                if shuRuCuoWuCiShu == 2{
                    //重新生成新的图片
                    shengChengTuPianBingFuZhi()
                    //背景色恢复正常
                    userInputNumber?.backgroundColor = UIColor(red: 192/255,green: 192/255,blue: 192/255,alpha: 1)
                    shuRuCuoWuCiShu = 0
                    userInputNumber?.text = ""
                }

                print("buzhengque")
            }
            let ZhengQueLv = jiSuanZhengQueLv()
            zhengQueLvLabelValue?.text = "\(ZhengQueLv)%"
        }
        
        //计算单字练习速度并实时显示
        danZiLianXiSuDu = jiSuanLianXiSuDu()
        suDuLabelValue?.text = "\(danZiLianXiSuDu)字/分钟"
    }
    
    /*
     作者:张振东
     时间:2016-9-29
     功能:当点击键盘上的0的时候进行的相关操作
     */
    func clickZero(){
        
        if userInputNumber?.text?.characters.count < imageNameCount{
            userInputNumber?.text = (userInputNumber?.text)! + "0"
        }
        print(imageNameCount)
        if userInputNumber?.text?.characters.count == imageNameCount{
            //如果输入的字符串的长度和生成的图片的名称的长度相等,那么不管是相等还是不等,那么所有数都+1
            suoYouZhengShu = suoYouZhengShu+1
            print(userInputNumber?.text?.characters as Any)
            if userInputNumber?.text == jieQuImageNameDeDaoDianZhiQian(imageName!){
                print("zhengque")
                //正确的个数➕1
                zhengQueShu = zhengQueShu+1
                print(zhengQueShu)
                zhengQueShuLabelValue?.text = "\(zhengQueShu)"
                shengChengTuPianBingFuZhi()
                userInputNumber?.backgroundColor = UIColor(red: 192/255,green: 192/255,blue: 192/255,alpha: 1)
                userInputNumber?.text = ""
            }else{
                shuRuCuoWuCiShu = shuRuCuoWuCiShu+1
                userInputNumber?.backgroundColor = UIColor.red
                userInputNumber?.text = ""
                if shuRuCuoWuCiShu == 2{
                    //重新生成新的图片
                    shengChengTuPianBingFuZhi()
                    //背景色恢复正常
                    userInputNumber?.backgroundColor = UIColor(red: 192/255,green: 192/255,blue: 192/255,alpha: 1)
                    shuRuCuoWuCiShu = 0
                    userInputNumber?.text = ""
                }

                print("buzhengque")
            }
            //计算正确率并实时显示
            let ZhengQueLv = jiSuanZhengQueLv()
            zhengQueLvLabelValue?.text = "\(ZhengQueLv)%"
        }
        
        
        //计算单字练习速度并实时显示
        danZiLianXiSuDu = jiSuanLianXiSuDu()
        suDuLabelValue?.text = "\(danZiLianXiSuDu)字/分钟"
    }
    
    /*
     作者:张振东
     日期:2016-9-28
     功能:计算正确率
     */
    func jiSuanZhengQueLv() -> Double{
        
        zhengQueLv = (Double(zhengQueShu)/Double(suoYouZhengShu))*100
        //保留小数点后两位
        let zhengQueLvZhi = String(format: "%.2f", zhengQueLv)
        print(zhengQueLvZhi)
        return Double(zhengQueLvZhi)!
    }
    
    
    /*
     作者:张振东
     日期:2016-9-28
     功能:计算笔形训练练习速度
     */
    func jiSuanLianXiSuDu() -> Int{
        print(Int(suoYouZhengShu))
        print(Int(fenZhongZhi!)!)
        let lianXiSudu = Int(suoYouZhengShu)/Int(fenZhongZhi!)!
        print(lianXiSudu)
        return lianXiSudu
    }
    
    func genJuSelectedValueJiShi(){
        timerValue += 1
        print(fenZhongZhi as Any)
        
        //获得分钟
        let fenZhongValue = timerValue/60
        //获取秒数
        let miaoValue = timerValue%60
        
        if fenZhongValue < 10 {
            if miaoValue < 10 {
                shiJianLabelValue?.text = "00:0\(fenZhongValue):0\(miaoValue)"
            }else{
                shiJianLabelValue?.text = "00:0\(fenZhongValue):\(miaoValue)"
            }
        }else{
            if miaoValue < 10{
                shiJianLabelValue?.text = "00:\(fenZhongValue):0\(miaoValue)"
            }else{
                shiJianLabelValue?.text = "00:\(fenZhongValue):\(miaoValue)"
            }
        }
        
        //当计时结束时,可以在此进行if语句中显示最终的结果
        if timerValue == Int(fenZhongZhi!)! * 60{
            //倒计时结束
            timer.invalidate()
            //按钮失去交互性
            jianPanView?.isUserInteractionEnabled = false
            oneBtn?.isUserInteractionEnabled = false
            twoBtn?.isUserInteractionEnabled = false
            threeBtn?.isUserInteractionEnabled = false
            fourBtn?.isUserInteractionEnabled = false
            fiveBtn?.isUserInteractionEnabled = false
            sixBtn?.isUserInteractionEnabled = false
            sevenBtn?.isUserInteractionEnabled = false
            eightBtn?.isUserInteractionEnabled = false
            nineBtn?.isUserInteractionEnabled = false
            lingBtn?.isUserInteractionEnabled = false
            
            
            //当传递过来的时间消耗完毕,计时就要从0开始
            timerValue = 0
            //查看结果具有交互性
            chaKanJieGuoBtn?.isUserInteractionEnabled = true
            print("倒计时结束")
        }
    }
    
    /*
     作者:张振东
     日期:2016-09-29
     功能:显示倒计时时间
     */
    func genJuXuanZhongFenZhongZhiDaoJiShi(){
        print(fenZhongZhi!)
        print(timerValue)
        if timerValue <= 0{
            timerValue = 0
        }else{
            timerValue = timerValue - 1
            //获得分钟
            let minuteValue = timerValue/60
            //获得秒数
            let secondsValue = timerValue%60
            if minuteValue < 10{
                if secondsValue < 10{
                    shiJianLabelValue?.text = "00:0\(minuteValue):0\(secondsValue)"
                }else{
                    shiJianLabelValue?.text = "00:0\(minuteValue):\(secondsValue)"
                }
            }else{
                if secondsValue < 10{
                    shiJianLabelValue?.text = "00:\(minuteValue):0\(secondsValue)"
                }else{
                    shiJianLabelValue?.text = "00:\(minuteValue):\(secondsValue)"
                }
            }
            //当计时结束时,可以在此进行if语句中显示最终的结果
            if timerValue == 0{
                //表示倒计时结束
                timer.invalidate()
                //按钮失去交互性
                jianPanView?.isUserInteractionEnabled = false
                oneBtn?.isUserInteractionEnabled = false
                twoBtn?.isUserInteractionEnabled = false
                threeBtn?.isUserInteractionEnabled = false
                fourBtn?.isUserInteractionEnabled = false
                fiveBtn?.isUserInteractionEnabled = false
                sixBtn?.isUserInteractionEnabled = false
                sevenBtn?.isUserInteractionEnabled = false
                eightBtn?.isUserInteractionEnabled = false
                nineBtn?.isUserInteractionEnabled = false
                lingBtn?.isUserInteractionEnabled = false
                
                timerValue = 0
                
                //查看结果具有交互性
                chaKanJieGuoBtn?.isUserInteractionEnabled = true
                print("倒计时结束!!")
            }
        }
        
    }

    
    /*
     作者:张振东
     日期:2016-09-29
     功能:点击暂停所做的工作
     */
    func zanTing(){
        //查看结果按钮才具有交互性
        chaKanJieGuoBtn?.isUserInteractionEnabled = true
        zanTingBtn?.isSelected = !(zanTingBtn?.isSelected)!
        print(zanTingBtn?.isSelected as Any)
        if zanTingBtn?.isSelected == true{
            //按钮失去交互性
            jianPanView?.isUserInteractionEnabled = false
            oneBtn?.isUserInteractionEnabled = false
            twoBtn?.isUserInteractionEnabled = false
            threeBtn?.isUserInteractionEnabled = false
            fourBtn?.isUserInteractionEnabled = false
            fiveBtn?.isUserInteractionEnabled = false
            sixBtn?.isUserInteractionEnabled = false
            sevenBtn?.isUserInteractionEnabled = false
            eightBtn?.isUserInteractionEnabled = false
            nineBtn?.isUserInteractionEnabled = false
            lingBtn?.isUserInteractionEnabled = false
            
            
            //2、将上面的正确数、正确率、时间、速度保存在本地
            UserDefaults.standard.set(zhengQueShu, forKey: "zhengQueShuInt")
            UserDefaults.standard.set(zhengQueLv, forKey: "zhengQueLv")
            UserDefaults.standard.set(timerValue, forKey: "timerValue")
            UserDefaults.standard.set(danZiLianXiSuDu, forKey: "danZiLianXiSuDu")
            
            let a = UserDefaults.standard.integer(forKey: "timerValue")
            print(a)
            UserDefaults.standard.synchronize()
            //暂停两个字修改为开始
            zanTingBtn?.setTitle("开始", for: UIControlState())
            //暂停的按钮修改为开始按钮
            let kaiShiImage = UIImage(named: "begin")
            zanTingImageView!.image = kaiShiImage
            zanTingBtn?.setTitleColor(UIColor.gray, for: UIControlState())
            //让时间失去交互性
            timer.invalidate()
        }else{
            jianPanView?.isUserInteractionEnabled = true
            oneBtn?.isUserInteractionEnabled = true
            twoBtn?.isUserInteractionEnabled = true
            threeBtn?.isUserInteractionEnabled = true
            fourBtn?.isUserInteractionEnabled = true
            fiveBtn?.isUserInteractionEnabled = true
            sixBtn?.isUserInteractionEnabled = true
            sevenBtn?.isUserInteractionEnabled = true
            eightBtn?.isUserInteractionEnabled = true
            nineBtn?.isUserInteractionEnabled = true
            lingBtn?.isUserInteractionEnabled = true
                       //让暂停恢复原状
            zanTingBtn?.setTitle("暂停", for: UIControlState())
            let zanTingImage = UIImage(named: "zanting")
            zanTingImageView!.image = zanTingImage
            zanTingBtn?.setTitleColor(UIColor.black, for: UIControlState())
            
            //当开始的时候,查看结果按钮是不具有交互性的
            chaKanJieGuoBtn?.isUserInteractionEnabled = false
            //计时器开始正常工作
            if let moShiCategory = moShiLeiBie{
                print(moShiCategory)
                if moShiCategory == "计时"{
                    timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(genJuSelectedValueJiShi), userInfo: nil, repeats: true)
                }else if moShiCategory == "定时"{
                    timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(genJuXuanZhongFenZhongZhiDaoJiShi), userInfo: nil, repeats: true)
                }
            }
        }

    }
    
    /*
     作者:张振东
     日期:2016-09-29
     功能:点击查看结果按钮查看结果
     */
    func chaKanJieGuo(){

        let danZiChaKanJieGuoViewController = DanZiChaKanJieGuoViewController()
        if let moShiCategory = moShiLeiBie{
            print(moShiCategory)
            danZiChaKanJieGuoViewController.moShiCategory = moShiCategory
            danZiChaKanJieGuoViewController.miniteValue = Int(fenZhongZhi!)!
        }
        self.navigationController?.pushViewController(danZiChaKanJieGuoViewController, animated: true)
    }
    
    /*
     作者:张振东
     日期:2016-09-29
     功能:点击结束所做的工作
     */
    func jieShu(){
        //1、时间失效
        timer.invalidate()
        //按钮失去交互性
        jianPanView?.isUserInteractionEnabled = false
        oneBtn?.isUserInteractionEnabled = false
        twoBtn?.isUserInteractionEnabled = false
        threeBtn?.isUserInteractionEnabled = false
        fourBtn?.isUserInteractionEnabled = false
        fiveBtn?.isUserInteractionEnabled = false
        sixBtn?.isUserInteractionEnabled = false
        sevenBtn?.isUserInteractionEnabled = false
        eightBtn?.isUserInteractionEnabled = false
        nineBtn?.isUserInteractionEnabled = false
        lingBtn?.isUserInteractionEnabled = false
        
        chaKanJieGuoBtn?.isUserInteractionEnabled = true
        //3、将上面的正确数、正确率、时间、速度保存在本地
        UserDefaults.standard.set(zhengQueShu, forKey: "zhengQueShuInt")
        UserDefaults.standard.set(zhengQueLv, forKey: "zhengQueLv")
        UserDefaults.standard.set(timerValue, forKey: "timerValue")
        UserDefaults.standard.set(danZiLianXiSuDu, forKey: "danZiLianXiSuDu")

    }

    /*
     作者:张振东
     时间:2016-9-25
     功能:封装键盘上的按钮的属性
     */
    func fengZhuangShuZiButtonAttributes(_ button:UIButton){
        
        button.setTitleColor(UIColor.black, for: UIControlState())
        button.titleLabel?.font = UIFont.systemFont(ofSize: 25)
        switch button {
        case oneBtn!:
            button.setTitle("1", for: UIControlState())
            break
        case twoBtn!:
            button.setTitle("2", for: UIControlState())
            break
        case threeBtn!:
            button.setTitle("3", for: UIControlState())
            break
        case fourBtn!:
            button.setTitle("4", for: UIControlState())
            break
        case fiveBtn!:
            button.setTitle("5", for: UIControlState())
            break
        case sixBtn!:
            button.setTitle("6", for: UIControlState())
            break
        case sevenBtn!:
            button.setTitle("7", for: UIControlState())
            break
        case eightBtn!:
            button.setTitle("8", for: UIControlState())
            break
        case nineBtn!:
            button.setTitle("9", for: UIControlState())
            break
        case dianBtn!:
            button.setTitle(".", for: UIControlState())
            button.backgroundColor = UIColor(red: 200/255,green: 199/255,blue: 204/255,alpha: 1)
            break
        case lingBtn!:
            button.setTitle("0", for: UIControlState())
            break
        default:
            print("--")
            break
        }
    }

    
    /*
     作者:张振东
     时间:2016-9-29
     功能:封装UIlabel对象的属性®
     */
    @discardableResult
    func fengZhuangLabelAttributes(_ label:UILabel) -> UILabel{
        
        label.font = UIFont.systemFont(ofSize: 12)
        label.textAlignment = .center
        
        switch label {
        case zhengQueShuLabel!:
            label.text = "正确数"
        case zhengQueShuLabelValue!:
            label.text = "\(zhengQueShu)"
        case zhengQueLvLabel!:
            label.text = "正确率"
        case zhengQueLvLabelValue!:
            label.text = "0%"
        case shiJianLabel!:
            label.text = "时间"
        case shiJianLabelValue!:
            label.text = "00:00:00"
        case suDuLabel!:
            label.text = "速度"
        case suDuLabelValue!:
            label.text = "0字/分钟"
            
        default: break
            
        }
        return label
    }

    
    /*
        作者:张振东
        时间:2016-9-29
        功能:封装键盘上的按钮的UIView对象的背景色
     */
    func fengZhuangJianPanUiView(_ view:UIView){
        view.backgroundColor = UIColor(red: 200/255,green: 199/255,blue: 204/255,alpha: 1)
    }

    
    /*
        作者:张振东
        时间:2016-9-28
        功能:禁用系统键盘
     */
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        return false
    }
    
    //返回到上一个页面
    func backToPrevious(){
        self.navigationController?.popViewController(animated: true)
    }

    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
